package com.thinklab.view3g;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ReportActivity extends ListActivity {
	V3Common mCommon;
	TextView mDate , mDown, mUp;
	BroadcastReceiver mRefresher;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.report);
		setListAdapter(new EfficientAdapter(this));
		
		mCommon = V3Common.getInstance(getApplicationContext());
		
		//LoadInfomation();
		
		registerReceiver(mRefresher = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				setListAdapter(new EfficientAdapter(ReportActivity.this));
			}
		},new IntentFilter(V3NetworkService.USAGE_REFRESHED));
		
		Button reset = (Button)findViewById(R.id.reset_button);
		reset.setOnClickListener(new OnClickListener(){				
			public void onClick(View v) {
				AlertDialog alert = new AlertDialog.Builder(ReportActivity.this)
				.setTitle(getString(R.string.alert_init_title))
				.setMessage(getString(R.string.alert_init_body))
				.setIcon(getResources().getDrawable(R.drawable.icon_alert))
				.setCancelable(true)
				.setPositiveButton("Yes", new DialogInterface.OnClickListener() {			
					public void onClick(DialogInterface dialog, int which) {
						mCommon.set3GReceive(0);
						mCommon.set3GTransmit(0);
						mCommon.initStartDate();
						setListAdapter(new EfficientAdapter(ReportActivity.this));
					}
				})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						
					}
				})
				.show();
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		unregisterReceiver(mRefresher);
		super.onDestroy();
	}

    private class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private Drawable mIcon0;
        private Drawable mIcon1;
        private Drawable mIcon2;
        private Context mContext;
        
        public EfficientAdapter(Context context) {
        	mContext = context;
        	
            mInflater = LayoutInflater.from(context);
            mIcon0 = context.getResources().getDrawable(R.drawable.icon_date);
            mIcon1 = context.getResources().getDrawable(R.drawable.icon_down);
            mIcon2 = context.getResources().getDrawable(R.drawable.icon_up);
        }
        
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.report_list_item, null);
            }
            
        	TextView label = (TextView) convertView.findViewById(R.id.list_label);
        	TextView text = (TextView) convertView.findViewById(R.id.list_Text);
        	ImageView icon = (ImageView) convertView.findViewById(R.id.list_icon);
        	switch(position){
        	case 0:
	        	{
	        		long systime = mCommon.getStartDate();
	        		String time = DateFormat.format("MMMM dd, yyyy h:mmaa", systime).toString();
	        		label.setText(mContext.getString(R.string.report_start_date));
	        		text.setText(time);
	        		icon.setBackgroundDrawable(mIcon0);        		
	        		break;
	        	}
        	case 1:
	        	{
	        		label.setText(mContext.getString(R.string.report_download));
	        		String kb = String.format("%.1f", mCommon.get3GReceive()/1024.0);
	        		String mb = String.format("%.1f", mCommon.get3GReceive()/1024.0/1024.0);
	        		text.setText(kb + "KB = " + mb + "MB");
	        		icon.setBackgroundDrawable(mIcon1);
	        		break;
	        	}
        	case 2:
        		{
	        		label.setText(mContext.getString(R.string.report_upload));
	        		String kb = String.format("%.1f", mCommon.get3GTransmit()/1024.0);
	        		String mb = String.format("%.1f", mCommon.get3GTransmit()/1024.0/1024.0);
	        		text.setText(kb + "KB = " + mb + "MB");
	        		icon.setBackgroundDrawable(mIcon2);
	        		break;
        		}
        	}
        	
            return convertView;
        }

		public int getCount() {
			return 3;
		}

		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}
    }

}
